'use strict';
const Service = require('../core/base_service.js');
class WorkService extends Service {

  /** 
   * @description 查询work
   * @param {*} data {
   *                     pageSize
   *                     pageNo
   *                     startTime
   *                     endTime
   *                  }
   * @returns
   * @memberof UserService
å   */
  async findWork(data) {

    console.log("in findwork__________________________")
    const now_time = this.app.mysql.literals.now;
    let  results = null ;
    console.log("find work =+++++++")
    console.log(data)
    //添加默认值
    const filter_data = Object.assign({
      startTime:now_time,
      endTime:now_time,
      pageNo:1,
      pageSize:10
  },data)
  
    console.log("filter_data+++++++")
    console.log(filter_data)

    //筛选时间
    const where_time =   {time:filter_data.startTime - filter_data.endTime};

    const select_sql = "select * from test limit "+(filter_data.pageNo-1)*filter_data.pageSize+","+filter_data.pageSize;

    const allRows = "select count(*) from test";

    // results = await this.app.mysql.select('test', {
    //   where: {
    //     id: 1,
    //   }, // WHERE 条件
    //   columns: ['id', 'title'], // 要查询的表字段
    //   limit: 10, // 返回数据量
    // });
  let rowData = null;
    try{
      results  = await this.app.mysql.query(select_sql);
      rowData = await this.app.mysql.query(allRows)
       console.log("--------")
       console.log(results)
      //  results.total = rowData
       console.log("allRows")
       console.log(allRows)
       console.log(results)
    }catch(e){
      console.log(e)
    }
    

    console.log("select workDetail________________")
    console.log(results)
    return {results,total:rowData[0]['count(*)']};  
  }


  async getBanner() {
    const result = await this.app.mysql.select('banner'); // 更新 posts 表中的记录
    console.log("getbanner",result);
    const getBannerSUcc = result.length>0;
    return result;
  }

  async updateBanner(data) {
    // const row = {
    //   id: data.id,
    //   bannerImg: data.bannerImg,
    //   // time:this.app.mysql.literals.now
    // };
    console.log("updataBanner",data);
    const result = await this.app.mysql.update('banner', data[0]); // 更新 posts 表中的记录
    const result2=await this.app.mysql.update('banner', data[1]); // 更新 posts 表中的记录
    const updateSuccess = result.affectedRows === 1;
    const result2S = result2.affectedRows ===1
    return updateSuccess && result2S;
  }

  async CreateWork(data){
    console.log("this.app.mysql.literals.now",this.app.mysql.literals.now)
    const row = {
      title:data.title,
      work: data.work,
      time:this.app.mysql.literals.now,
      img:data.img
    };
    // const result = await this.app.mysql.insert('test',row);
   const result =  await this.app.mysql.query(`INSERT INTO test SET title = ?, work = ?,time = now(), img=?`, [data.title, data.work,data.img]);
    const updateSuccess = result.affectedRows === 1;
    return updateSuccess;
  }
  
  async getDetailWork(data){
    let result = null
    console.log("data")
    console.log(data)
     try{
        result = await this.app.mysql.get('test', { id: data.id });
     }catch(e){
       console.log(e)
     }
     
     console.log("result")
     console.log("getDetailWork")
     console.log(result)
    //  const updateSuccess = result.affectedRows === 1;
     return result;
   }
}



module.exports = WorkService;